// pages/WriteAnInvoice/WriteAnInvoice.js
let that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectindex:1,
    forName:'',
    taxCode:'',
    brief:'',
    linkPhone:'',
    isforName:!1,
    istaxCode:!1,
    islinkPhone:!1,
    isbrief:!1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.getdata()
   
  },
  getdata(){
    let page=getCurrentPages(),
    data=page[page.length-2].data;
    console.log(data,'datadatadata');
    that.setData({data})
  },
  inputtap(e){
    let { value } = e.detail,{prop}=e.currentTarget.dataset
    that.setData({[prop]:value})
  },
  to(){
    let { selectindex, forName, taxCode, brief,data,linkPhone}=that.data,err='',idarr=[]
    
    if(linkPhone=='')err='请输入联系电话'
    if(selectindex==1){
      if(taxCode=='')err='请输入税号'
      if(forName=='')err='请输入发票抬头'
    }
    if(err!=''){
      wx.showModal({
        content:err,
        showCancel:!1,
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    data.orderRecordlist.forEach(x=>{
      if(x.state){
        idarr.push(x.id)
      }
    })
    let apidata={applyFee:data.applyFee,ids:idarr.toString(),reTypeId:2,forName, taxCode, brief,linkPhone,
    cateId:data.cateId,invoiceapply:data.invoiceapply,stationId:data.stationId}
    api.invoiceapply(apidata,res=>{
      wx.hideLoading()
      if(res.data.code==0){
        wx.redirectTo({
          url: '../InvoiceSucc/InvoiceSucc?stationId='+data.stationId+'&code=1',
        })
      }else{
        wx.showModal({
          content:res.data.message,
          showCancel:!1,
        })
      }
    })
  },
  toselect(e){
    let { index } = e.currentTarget.dataset, { selectindex } = that.data;
    if (selectindex == index) {
      return;
    } 
    this.setData({ selectindex: index},()=>{
    });
  },
  isfocus(e){
    console.log(e,'eeeee');
    let {code}=e.currentTarget.dataset
    that.setData({[code]:!0})
  },
  bindblur(e){
    console.log(e,'eeeee');
    let {code}=e.currentTarget.dataset
    that.setData({[code]:!1})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})